/**
 * @file 数值处理
 * @author dafo<huanghoujin@baijia.com>
 */

/**
 * 补零
 *
 * @param {number} number 输入的数字
 * @return {string} 补零为两位的字符
 */
export const addZero = number => {
    if (typeof number !== 'number') {
        number = Number(number);
    }

    if (number < 10) {
        return `0${number}`;
    }

    return String(number);
};

/**
 * 格式化价格
 *
 * @param {number|string} price 单位为分的价格
 * @return {number} 单位转为元后的价格
 */
export const formatPrice = price => ((price * 100) | 0) / 100 / 100;

/**
 * 限时折扣价格展示
 *
 * @param {Object} discount 优惠折扣
 * @param {number|string} originalPrice 原价
 * @param {number} price 价格
 * @param {number} countDownStatus 倒计时状态
 * @return {Object} 经过计算后显示的价格信息
 */
export const priceHander = (
    discount,
    originalPrice,
    price,
    countDownStatus
) => {
    const showPriceObj = {};

    // 没走折扣价判断时return；
    if (countDownStatus === 0) {
        return showPriceObj;
    }

    if (discount) {
        // 有限时折扣区分是预告还是折扣中  1 = 预告； 2 = 折扣中； 3 = 折扣过期；
        switch (countDownStatus) {
            case 1:
                showPriceObj.type = countDownStatus;
                showPriceObj.nowPrice = price;
                showPriceObj.prevPrice = discount.price;
                break;
            case 2:
                showPriceObj.type = countDownStatus;
                showPriceObj.nowPrice = discount.price;
                showPriceObj.prevPrice = price;
                break;
            case 3:
                showPriceObj.type = countDownStatus;
                showPriceObj.nowPrice = price;
                showPriceObj.prevPrice = originalPrice || '';
                break;
            default:
                break;
        }
    } else {
        showPriceObj.type = 3;
        showPriceObj.nowPrice = price;
        showPriceObj.prevPrice = originalPrice || '';
    }

    return showPriceObj;
};

export const toFixed2 = value => parseFloat(value).toFixed(2);

// 给regFenToYuan专用的方法, 因为金额中间有',', 所以这里用'.'区分;
// 格式化数字两位小数用toFixed2
const toDecimal2 = x => {
    let numStr = x;
    const Index = numStr.indexOf('.');
    if (Index === -1) {
        return `${numStr}.00`;
    }
    const split = numStr.substring(Index + 1);
    if (split.length === 1) {
        numStr = `${numStr}0`;
    }
    return numStr;
};

export const regFenToYuan = fen => {
    let num = fen;
    num = fen / 100;
    num = num.toString().indexOf('.') === -1
        ? num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
        : num.toLocaleString();
    num = toDecimal2(num);
    num = num.slice(0, num.toString().indexOf('.'));
    return num;
};

/** 符合常识的安全的js乘法, 返回字符串. 会去掉小数部分末尾的0, 如果小数部分全为0, 去掉小数点和小数部分 */
export const safeMultiple = (num1, num2) => {
    const numstr1 = String(num1);
    const numstr2 = String(num2);
    const split1 = numstr1.split('.');
    const split2 = numstr2.split('.');
    const frac1 = split1?.[1]?.length ?? 0;
    const frac2 = split2?.[1]?.length ?? 0;
    const result = parseFloat(num1) * parseFloat(num2);
    const split3 = String(result).split('.');
    if (split3?.length === 1) {
        return String(result);
    }
    return result.toFixed(frac1 + frac2).replace(/(\.0+|0+|\.)$/, '');
};

// 百分比 number 转成百分比
export const parsePercent = (num, fixed = 2) => {
    if (typeof num === 'number') {
        return `${(num * 100).toFixed(fixed)}%`;
    }
    const result = Number(num);
    if (Number.isNaN(result)) {
        return '-';
    }
    return parsePercent(result, fixed);
};

// 数值万、亿
export const numberHandle = (number, fixed = 0) => {
    let num = Math.abs(number);

    // 遍历数据时，带百分号的直接返回
    if (isNaN(num)) {
        return number;
    }

    const yi = 100000000;
    const wan = 10000;
    let unit = '';
    switch (true) {
        case num >= yi:
            unit = '亿';
            num = (num / yi).toFixed(fixed);
            break;
        case num < yi && num >= wan:
            unit = '万';
            num = (num / wan).toFixed(fixed);
            break;
        case num < wan && String(num).indexOf('.') > -1 && fixed >= 0:
            num = Number(num).toFixed(fixed);
            break;
        default:
            num = Number(num);
    }
    return number >= 0 ? `${num}${unit}` : `-${num}${unit}`;
};

export const strToNumber = str => {
    const result = Number(str);

    if (isNaN(result)) {
        return '-';
    }

    return result;
};
